package top.roadvast.pdshopping.rabbitmq;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;
import top.roadvast.pdshopping.bean.PdLog;
import top.roadvast.pdshopping.service.LogService;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @Author 郭泽鹏
 * @Date 2021/12/27 20:50
 * @Version 1.0
 * @Description rabbitmq监听器
 * 广播模式：指定交换机，队列，bindingKey为空
 * 收到日志数据后,会调用日志的业务代码,把日志保存到数据库
 */
@Component
@Slf4j
public class FanoutLogConsumer {
    @Resource
    LogService logService;


//    //消费者01
//    @RabbitListener(bindings = @QueueBinding(
//            value = @Queue(value = "pd_log_fanout_queue01"),
//            exchange = @Exchange(value = "pd_log_fanout_exchange", type = ExchangeTypes.FANOUT, durable = "true"),
//            key = ""))
//    public void save(PdLog pdLog, Channel channel, Message message) {
//        try {
//            log.info("接收到日志，开始进行处理...");
//            logService.saveLog(pdLog);
//            //手动确认
//            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }

    //消费者02
    @RabbitListener(bindings = {@QueueBinding(
            value = @Queue(value = "pd_log_fanout_queue02"),
            exchange = @Exchange(value = "pd_log_fanout_exchange", type = ExchangeTypes.FANOUT, durable = "true"),
            key = "")}
    )
    public void printLog(PdLog pdLog, Channel channel, Message message) {
        try {
            log.info("接收到日志，开始进打印...");
            log.info("订单信息：" + pdLog);
            //手动确认
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
